package org.springframework.utils;

import java.io.IOException;

import mx4j.tools.adaptor.http.HttpAdaptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.jmx.export.MBeanExporter;

/**
 * MBeanExporter初始化后再启动httpAdaptor
 * 
 * @author Administrator
 * 
 */
public class HttpAdaptorBeanPost implements BeanPostProcessor {

	private static final Logger logger = LoggerFactory
			.getLogger(HttpAdaptorBeanPost.class);
	private HttpAdaptor httpAdaptor;

	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		if (bean instanceof HttpAdaptor) {
			httpAdaptor = (HttpAdaptor) bean;
		}
		if (bean instanceof MBeanExporter) {
			if (httpAdaptor != null) {
				try {
					httpAdaptor.start();
				} catch (IOException e) {
					logger.error("httpAdaptor start error !", e);
				}
			}
		}
		// System.out.println("对象" + beanName + "实例化完成");
		return bean;
	}

	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		// System.out.println("对象" + beanName + "开始实例化");
		return bean;
	}
}
